﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using TorchView.PakViewer.Zip;
using TorchView.Imaging;
using System.Windows.Media.Imaging;

namespace TorchView.PakViewer
{
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            IZipNode node = (IZipNode)value;
            if (!node.IsDirectory)
            {
                ZipNode zip = (ZipNode)node;

                if (zip.Name.EndsWith(".dds", StringComparison.CurrentCultureIgnoreCase))
                {
                    DDSLoader dds = new DDSLoader();
                    return dds.Load(zip.OpenFile());
                }

                if (zip.Name.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase))
                {
                    PngBitmapDecoder decoder = new PngBitmapDecoder(zip.OpenFile(), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    return decoder.Frames[0];
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
        }
    }
}
